package com.xxx.car.web;

import com.xxx.car.entity.vo.request.ReqETC;
import com.xxx.car.entity.vo.request.ReqEndTravel;
import com.xxx.car.entity.vo.request.ReqStartTravel;
import com.xxx.car.entity.vo.response.RespAlarm;
import com.xxx.car.entity.vo.response.RespCallback;
import com.xxx.car.service.TravelAppealService;
import com.xxx.car.utils.GsonUtils;
import com.xxx.common.base.enums.ApiResponseEnum;
import com.xxx.common.base.vo.APIResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by youxiaojia on 2017/6/13.
 */
@RestController
@RequestMapping("/travel")
public class TravelController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    TravelAppealService travelAppealService;

	/**
     * ETC进站回调
     * @return
    @RequestMapping(value = "/begin")
    public APIResponse<?> begin(@Valid @ModelAttribute ReqStartTravel reqStartTravel,
                                BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        try {
            return travelAppealService.createStartTravel(reqStartTravel.getCarNo(),reqStartTravel.getGaosuName(),reqStartTravel.getRoadName(),reqStartTravel.getStateName());
        } catch (Exception e){
            return APIResponse.returnFail(e.getMessage());
        }
    }*/


	/**
     * ETC出站回调
     * @return
    @RequestMapping(value = "/end")
    public APIResponse<?> end(@Valid @ModelAttribute ReqEndTravel reqEndTravel,
                              BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        try {
            return travelAppealService.finishTravel(reqEndTravel.getCarNo(),reqEndTravel.getGaosuName(),reqEndTravel.getRoadName(),reqEndTravel.getStateName(),reqEndTravel.getDistance(),reqEndTravel.getPrice());
        }catch (Exception e){
            return APIResponse.returnFail(e.getMessage());
        }
    }*/

    /**
     * ETC联调callback
     * @return
     */
    @RequestMapping(value = "/callback")
    public String etcCallback(@RequestBody ReqETC req, HttpServletRequest request) {
        ReqETC reqETC = null;

        Map map = request.getParameterMap();
        String etcPara = null;
        Set set = map.keySet();
        Iterator iter = set.iterator();
        while (iter.hasNext()) {
            etcPara = (String) iter.next();
            // printkey
        }
        if (etcPara != null){
            String trimStr = etcPara.replaceAll("\\s*","");
            logger.error("ETC入参:"+trimStr);
            reqETC = GsonUtils.jsonStrToEntity(trimStr,ReqETC.class);
        } else {
            reqETC = req;
            logger.error("ETC入参:"+GsonUtils.toJson(reqETC));
        }
        try {
            if (reqETC.getAlarmInfoPlate().getResult().getPlateResult().getLicense() != null){
                String resp = travelAppealService.createStartTravel(reqETC.getAlarmInfoPlate().getResult().getPlateResult().getLicense(),"demo高速","demo路段","demo站点");
                logger.error("返回开闸数据:"+resp);
                return resp;
            } else {
                RespCallback respCallback = new RespCallback();
                RespAlarm respAlarm = new RespAlarm();
                respAlarm.setInfo("err");
                respAlarm.setContent("车牌号为空");
                respCallback.setResponse_AlarmInfoPlate(respAlarm);
                String resp = GsonUtils.toJson(respCallback);
                logger.error("返回开闸数据:"+resp);
                return resp;
            }
        }catch (Exception e){
            RespCallback respCallback = new RespCallback();
            RespAlarm respAlarm = new RespAlarm();
            respAlarm.setInfo("err");
            respAlarm.setContent(e.getMessage());
            respCallback.setResponse_AlarmInfoPlate(respAlarm);
            String resp = GsonUtils.toJson(respCallback);
            logger.error("返回开闸数据:"+resp);
            return resp;
        }
    }
}
